﻿using System.Windows;

namespace SS.WPF.Framework.Markup
{
    #region Handlers for RoutedEvent

    /// <summary>
    /// Defines Handler for RecheckDirtyEvent
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    public delegate void RecheckDirtyHandler(object sender, RecheckDirtyRoutedEventArgs args);

    public delegate void DirtyChangedHandler(object sender, DirtyChangedRoutedEventArgs args);

    public delegate void NotifyDirtyHandler(object sender, DirtyRoutedEventArgs args);

    /// <summary>
    /// Event args for PreviewDirtyCheck event
    /// </summary>
    public class DirtyChangedRoutedEventArgs : RoutedEventArgs
    {
        private readonly string _group;

        private readonly string _propertyName;

        private readonly DirtyAction _action;

        public DirtyChangedRoutedEventArgs(RoutedEvent routedEvent, string group, string propertyName, DirtyAction action)
            : base(routedEvent)
        {
            _group = group;
            _propertyName = propertyName;
            _action = action;
        }

        public string PropertyName
        {
            get
            {
                return this._propertyName;
            }
        }

        public DirtyAction DirtyAction
        {
            get
            {
                return this._action;
            }
        }

        public string Group
        {
            get
            {
                return this._group;
            }
        }
    }

    /// <summary>
    /// Event args for RecheckDirtyEvent
    /// </summary>
    public class RecheckDirtyRoutedEventArgs : RoutedEventArgs
    {
        public RecheckDirtyRoutedEventArgs(RoutedEvent routedEvent)
            : base(routedEvent)
        {
        }
    }
    
    /// <summary>
    /// Event args for DirtyRoutedEventArgs
    /// </summary>
    public class DirtyRoutedEventArgs : RoutedEventArgs
    {
        public bool IsDirty { get; private set; }
        public DirtyRoutedEventArgs(bool isDirty)
        {
            IsDirty = isDirty;
        }
    }

    #endregion

    public class DirtyChangeChangedHandler
    {
        private readonly FrameworkElement _element;

        public DirtyChangeChangedHandler(FrameworkElement element)
        {
            this._element = element;
        }

        public void NotifyDirtyHandler(object sender, DirtyRoutedEventArgs args)
        {
            if (this._element != null)
            {
                var recheckDirtyRoutedEventArgs = new RecheckDirtyRoutedEventArgs(DirtyChecker.RecheckDirtyEvent);
                this._element.RaiseEvent(recheckDirtyRoutedEventArgs);
            }
        }
    }
}
